package com.peas.platform.module.icims.base.auth.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peas.platform.common.core.dto.BaseDto;
import com.peas.platform.common.core.dto.ResponseDto;
import com.peas.platform.framework.mybatisplus.utils.PageUtil;
import com.peas.platform.module.icims.base.auth.api.resp.UserResp;
import com.peas.platform.module.icims.base.auth.controller.criteria.UserCriteria;
import com.peas.platform.module.icims.base.auth.dto.UserDto;
import com.peas.platform.module.icims.base.auth.dto.UserRoleDto;
import com.peas.platform.module.icims.base.auth.service.UserRoleService;
import com.peas.platform.module.icims.base.auth.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "自定义接口-用户")
@RestController
@RequestMapping("/api/v1/customUser")
public class CustomUserController {

    @Resource
    private UserService userService;
    @Resource
    private UserRoleService userRoleService;

    @ApiOperation(value = "查询用户（分页）")
    @GetMapping("/queryUser")
    public ResponseDto<Page<UserResp>> queryUser(UserCriteria condition,
                                                 @ApiParam(name = "pageNum", value = "分页页码", example = "1") Integer pageNum,
                                                 @ApiParam(name = "pageSize", value = "分页每页大小", example = "10") Integer pageSize,
                                                 @ApiParam(name = "sortField", value = "排序字段", example = "id") String sortField,
                                                 @ApiParam(name = "sortType", value = "排序方式", example = "desc") String sortType) {
        Page<UserDto> pager = PageUtil.genPager(pageNum, pageSize, sortField, sortType);
        Map<String, Object> c = BeanUtil.beanToMap(condition);
        if (StrUtil.isNotBlank(condition.getRoleIds())) {
            List<UserRoleDto> list = userRoleService.listByField("roleId", condition.getRoleIds());
            c.put("id", list.stream().map(UserRoleDto::getUserId).collect(Collectors.toList()));
        }
        Page<UserDto> dtos = userService.page(pager, c);
        Page<UserResp> vos = PageUtil.copyPage(dtos, UserResp.class);
        if (CollUtil.isEmpty(vos.getRecords())) {
            return ResponseDto.getSuccessResponseDto(vos);
        }
        List<Long> userIds = dtos.getRecords().stream().map(BaseDto::getId).collect(Collectors.toList());
        List<UserRoleDto> urds = userRoleService.listByField("userId", userIds);
        vos.getRecords().forEach(user -> {
            List<Long> roleIds = urds.stream().filter(userRoleDto -> user.getId().equals(userRoleDto.getUserId())).map(UserRoleDto::getRoleId).collect(Collectors.toList());
            user.setRoleIds(roleIds);
        });
        return ResponseDto.getSuccessResponseDto(vos);
    }

}
